package com.zhengdongzulin.zuke.app.login

import androidx.lifecycle.rxLifeScope
import com.lxj.androidktx.livedata.StateLiveData
import com.zhengdongzulin.zuke.app.Api
import com.zhengdongzulin.zuke.app.data.ApartmentType
import com.zhengdongzulin.zuke.app.data.ChangeHomeData
import com.zhengdongzulin.zuke.app.data.UserInfo
import com.zhengdongzulin.zuke.base.BaseViewModel
import rxhttp.delay
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

class LoginVm : BaseViewModel() {

    var renter_user_login: StateLiveData<UserInfo> = StateLiveData()

    fun renter_user_login(phone: String, pwd: String = "", code: String = "") {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.RENTER_USER_LOGIN)
                    .add("phone", phone)
                    .add("password", pwd)
                    .add("code", code)
                    .toResponse<UserInfo>()
                    .delay(500)
                    .await()
                renter_user_login.postValueAndSuccess(r)
            }, {
                assignment(it, renter_user_login)
            }
        )
    }

    var renter_get_user_info: StateLiveData<UserInfo> = StateLiveData()

    fun renter_get_user_info(accessToken: String) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.RENTER_GET_USER_INFO)
                    .add("accessToken", accessToken)
                    .toResponse<UserInfo>()
                    .delay(500)
                    .await()
                renter_get_user_info.postValueAndSuccess(r)
            }, {
                assignment(it, renter_get_user_info)
            }
        )
    }

    var send_code: StateLiveData<String> = StateLiveData()

    fun send_code(phone: String) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.SEND_CODE)
                    .add("type", "1")
                    .add("phone", phone)
                    .toResponse<String>()
                    .await()
                send_code.postValueAndSuccess(r)
            }, {
                assignment(it, send_code)
            }
        )
    }


    var web_account_get_list: StateLiveData<MutableList<ApartmentType>> = StateLiveData()

    fun web_account_get_list() {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.WEB_ACCOUNT_GET_LIST)
                    .toResponse<MutableList<ApartmentType>>()
                    .await()
                web_account_get_list.postValueAndSuccess(r)
            }, {
                assignment(it, web_account_get_list)
            }
        )
    }


    var contract_list_house: StateLiveData<MutableList<ChangeHomeData>> = StateLiveData()

    fun contract_list_house() {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.CONTRACT_LIST_HOUSE)
                    .toResponse<MutableList<ChangeHomeData>>()
                    .await()
                contract_list_house.postValueAndSuccess(r)
            }, {
                assignment(it, contract_list_house)
            }
        )
    }

}